package mao.thinkinjava.io;



import java.io.*;

/**
 * 实现Externalizable实现自定义的外部化过程。
 */
public class Blip3  implements Externalizable {
    private int i;
    private String s;
    public Blip3(){
        System.out.println("default constructor");
    }
    public Blip3(int i,String s){
        System.out.println("constructor 2");
        this.i=i;
        this.s=s;
    }
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        System.out.println("write eternal");
       out.writeObject(i);
       out.writeObject(s);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        System.out.println("read external");
        i= (int) in.readObject();
        s =(String) in.readObject();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("Blip3:");
        builder.append(i);
        builder.append(s);
        return builder.toString();
    }

    public static void main(String[] args) throws Exception {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("blip.out"));
        Blip3 blip = new Blip3(2,"a blip");
        System.out.println(blip);
        out.writeObject(blip);
        out.close();

        ObjectInputStream in = new ObjectInputStream(new FileInputStream("blip.out"));
        Blip3 b = (Blip3) in.readObject();
        in.close();
    }
}
